package com.yvon.boot.log.event;

import com.yvon.boot.log.model.SystemLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;

import java.util.Objects;
import java.util.function.Consumer;

/**
 * @author : Yvon
 * @since : 2022-05-12
 */
@Slf4j
public class LogListener {

    private final Consumer<SystemLog> consumer;

    public LogListener(Consumer<SystemLog> consumer) {
        this.consumer = consumer;
    }

    @Async
    @Order
    @EventListener(LogEvent.class)
    public void saveSysLog(LogEvent event) {
        SystemLog commonLog = (SystemLog) event.getSource();
        if (Objects.nonNull(commonLog)) {
            this.consumer.accept(commonLog);
        }
    }
}
